package pers.qianyu.month_202012.date_20201230;

import org.junit.*;

import java.util.*;

/**
 * [Leetcode 每日一题]
 * 1046. 最后一块石头的重量
 * https://leetcode-cn.com/problems/last-stone-weight/
 *
 * @author mizzle rain
 * @date 2020-12-30 14:39
 */
public class LastStoneWeight {
    public int lastStoneWeight(int[] stones) {
        PriorityQueue<Integer> q = new PriorityQueue<>((i1, i2) -> i2 - i1);
        for (int stone : stones) {
            q.add(stone);
        }
        while (q.size() >= 2) {
            int x = q.poll();
            int y = q.poll();
            if (x != y) {
                q.offer(x > y ? x - y : y - x);
            }
        }
        return q.isEmpty() ? 0 : q.poll();
    }

    @Test
    public void test1() {
        int[] arr = {2, 7, 4, 1, 8, 1};
        int result = new LastStoneWeight().lastStoneWeight(arr);
        Assert.assertEquals(result, 1);
    }
}
